﻿using CompanyManagement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;

namespace CompanyManagement.usercontrol
{
    public partial class UcEditWard : System.Web.UI.UserControl
    {
        private object _dataItem = null;
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        #region Web Form Designer generated code
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        ///          Required method for Designer support - do not modify
        ///          the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.DataBinding += new System.EventHandler(this.CompanyDetails_DataBinding);

        }
        #endregion
        public object DataItem
        {
            get
            {
                return this._dataItem;
            }
            set
            {
                this._dataItem = value;
            }
        }

        protected void CompanyDetails_DataBinding(object sender, System.EventArgs e)
        {
            var listDistrict = new DistrictDAO().GetDistricts();
            ddlDistrict.DataSource = listDistrict;
            ddlDistrict.DataTextField = "Name";
            ddlDistrict.DataValueField = "ID";
            ddlDistrict.DataBind();
            //ListItem item = new ListItem("Chọn", "-1");
            //ddlDistrict.Items.Insert(0, item);
            // Edit data binding here
            if (!(DataItem is GridInsertionObject))
            {
                //// load district
                if (listDistrict.Count > 0)
                {
                    object district = DataBinder.Eval(DataItem, "District");
                    if (district == null)
                    {
                        ddlDistrict.SelectedIndex = 0;
                    }
                    else
                    {
                        ddlDistrict.SelectedIndex = listDistrict.IndexOf((District)district);
                        ddlDistrict.DataSource = null;
                    }
                }
            }
        }

        public string ProcessMyDataItem(object myValue)
        {
            if (myValue == null)
            {
                return "";
            }

            return myValue.ToString();
        }
    }
}